package wh.FishingPhilosopher.Components;

import java.util.Random;

import wh.FishingPhilosopher.GameParams.GameMathAlgorithm;
import wh.GameEngine.GameObject;
import android.graphics.PointF;
import android.graphics.RectF;

public class RandomAutoMoveComponent implements AutoMoveComponent{
	private Random random=new Random();
	private float fishMovingSpeed;
	private RectF activeArea; 
	public  RandomAutoMoveComponent(float fishMovingSpeed, RectF activeArea)
	{
		this.fishMovingSpeed=fishMovingSpeed;
		this.activeArea=activeArea;
	}
	public void autoMove(GameObject gameObject) {
		// TODO Auto-generated method stub
		
		PointF randomPosition;
		float randomAngle;
		int counter=0;
		do{
			if (counter>=5)
			{
				counter=0;
				gameObject.setBitmapAngle(90+gameObject.getBitmapAngle()>360?90+gameObject.getBitmapAngle()-360:90+gameObject.getBitmapAngle());
			}	
			randomAngle=getRandomAngleInMargin(gameObject.getBitmapAngle(), gameObject.getGameParams().randomAngleMargin);
			randomPosition=GameMathAlgorithm.calPositionAwayOfCenter(gameObject.getCenterPosition(), fishMovingSpeed, randomAngle);
			
			counter++;
		}
		while (!activeArea.contains(randomPosition.x,randomPosition.y));
		
		gameObject.setCenterPosition(randomPosition);
		gameObject.setBitmapAngle(randomAngle);
		
	}
	
	private float getRandomAngleInMargin(float originAngle, int margin) {
		// TODO Auto-generated method stub
		return (originAngle-margin/2+random.nextInt(margin));
	}
}
